package com.springcloud.setting.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author 
 */
public class Menu implements Serializable {
	
    private Integer id;

    /**
     * 父级ID
     */
    private Integer parentId;

    /**
     * 名称
     */
    private String name;

    /**
     * 排序
     */
    private Integer sort;

    /**
     * 链接
     */
    private String href;

    /**
     * 图标
     */
    private String icon;

    /**
     * 类型：0-菜单 1-子菜单 2-按钮
     */
    private Integer type;

    /**
     * 权限标识
     */
    private String permission;

    /**
     * 创建者
     */
    private String createBy;

    /**
     * 创建时间
     */
    private Date createDate;

    /**
     * 更新者
     */
    private String updateBy;

    /**
     * 更新时间
     */
    private Date updateDate;

    /**
     * 备注信息
     */
    private String remarks;

    /**
     * 删除标记
     */
    private String delFlag;
    
    private static final long serialVersionUID = 1L;
    
	/**
	 * 子菜单
	 */
	private List<Menu> children = new ArrayList<Menu>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }
    
	public List<Menu> getChildren() {
		return children;
	}

	public void setChildren(List<Menu> children) {
		this.children = children;
	}

	/**
	 * 
	 * <Description> 孩子节点排序<br>
	 */
	public void sortChildren() {
		Collections.sort(children, new Comparator<Menu>() {
			@Override
			public int compare(Menu menu1, Menu menu2) {
				int result = 0;

				Integer ordby1 = menu1.getSort();
				Integer ordby2 = menu2.getSort();

				Integer id1 = menu1.getId();
				Integer id2 = menu2.getId();
				if (null != ordby1 && null != ordby2) {
					result = (ordby1 < ordby2 ? -1 : (ordby1 == ordby2 ? 0 : 1));
				} else {
					result = (id1 < id2 ? -1 : (id1 == id2 ? 0 : 1));
				}
				return result;
			}

		});
		// 对每个节点的下一层节点进行排序
		for (Iterator<Menu> it = children.iterator(); it.hasNext();) {
			it.next().sortChildren();
		}
	}
	
	public static List<Menu> createTreeMenus(List<Menu> menus) {
        List<Menu> treeMenus = null;
        if (null != menus && !menus.isEmpty()) {
            // 创建根节点
            Menu root = new Menu();
            root.setName("菜单根目录");

            // 组装Map数据
            Map<Integer, Menu> dataMap = new HashMap<Integer, Menu>();
            for (Menu menu : menus) {
                dataMap.put(menu.getId(), menu);
            }

            // 组装树形结构
            Set<Entry<Integer, Menu>> entrySet = dataMap.entrySet();
            for (Entry<Integer, Menu> entry : entrySet) {
                Menu menu = entry.getValue();
                if (null == menu.getParentId() || 0 == menu.getParentId()) {
                    root.getChildren().add(menu);
                } else {
                    dataMap.get(menu.getParentId()).getChildren().add(menu);
                }
            }
            // 对树形结构进行二叉树排序
            root.sortChildren();
            treeMenus = root.getChildren();
        }
        return treeMenus;
    }
}